﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using CRMFramework;
//using Newtonsoft;
using Newtonsoft.Json;
using System.Text;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for Test1Handler
    /// </summary>
    public class ApplicationSelect : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
            var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);

            var sbSql = new StringBuilder("SELECT app_FirstName1,app_FirstName2,app_MiddleName1,app_MiddleName2,app_Surname1,app_Surname2,app_Date,app_Time");
            sbSql.Append(",(SELECT COUNT(1) FROM app_info) AS Total");
            sbSql.Append(" FROM app_info");
            sbSql.Append(" LIMIT ").Append((pageIndex - 1) * pageSize).Append(", ").Append(pageSize);

            var dt = DBhelper.ExecuteDatasetQuery(sbSql.ToString()).Tables[0];
            var total = (dt.Rows.Count > 0) ? Convert.ToInt32(dt.Rows[0]["Total"]) : 0;
            string strJson = JsonConvert.SerializeObject(dt);
            strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

            //context.Response.ContentType = "text/json";
            context.Response.Write(strJson);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}